%-------------------------------------------------------------------------
% Main File: State-Level Results 
%
% Estimation Codes for State-Level Results in  
% Ben-David, Itzhak, Sebastian Weber, and Pascal Towbin 
% "Inferring Expectations from Observables: Evidence from the Housing Market" 
% Review of Economics and Statistics
% ------------------------------------------------------------------------ 

%% DATA MANIPULATION
clear
close all

%% Set Directories
pwd
addpath 'auxilliary'

%% Load data
load data_state

%% Estimation Parameters
I=51;                % Number of States (includes DC)
t=size(crgdp,1)/I;   % Number of periods
p = 1;               % Number of lags
grdiff=1;            % Variable transformation for selct variables (to dirst dif)
n_rot_acc=5000;      % No. of req. acc. draws per state, default = 5000
lev = 0.16;          % Confidence band, default = 0.16
LL=1;                % Sign restriction horizon (start period)
KK=1;                % Sign restriction horizon (end period)
savedata=0;          % Save data from selected figures
outputfolder=pwd;    % Save data location

%% Estimation
estimation_identification_sta;         % Subroutine: Estimate SVAR and impose Sign Restrictions 

%% Defining variables and figure aspects
define_vars_sta;            % Subroutine: Data definitions 

%% Results for Full period 
comp_histdecomp_sta;        % Subroutine: Computation Historical Decomposition
boombustsize_sta;           % Subroutine: Compute size of boom and bust

histdecomp_figure_sta;      % Subroutine: Figures showing historical decomposition (Figure 5 a and b) 
Top20BoxWhiskerChart_sta;   % Subroutine: Figures showing shock contribution to housing boom (Figure 6 a to d) 
pc_analysis_sta             % Subroutine: Figures showing national component per shock (Figure 7)
boom_bust_scatter_sta;      % Subroutine: Scatter plots showing shock contributions and housing boom size (Figure 8)

%% Results for Old shock only
strerr=strerr_save;
strerr(:, peakdate:end,:,:)=0;
comp_histdecomp_sta;
fig_lab={'old shocks'};
hist_decomp_figure_old_new_sta;    % Subroutine: Figures showing contribution of old shocks (Figure 9 a and b)

%% Results for New shocks only
strerr=strerr_save;
strerr(:, 1:(peakdate-1),:,:)=0;
comp_histdecomp_sta;
fig_lab={'new shocks'};
hist_decomp_figure_old_new_sta;    % Subroutine: Figures showing contribution of new shocks (Figure 9 c and d)

%% End of File